package com.frank.httpdemo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.entity.InputStreamEntity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;

import com.frank.http.base.AsyncHttpClient;
import com.frank.http.base.RequestParams;
import com.frank.http.expand.CacheAsyncHttpClient;
import com.frank.http.expand.CacheAsyncHttpResponseHandler;

public class MainActivity1 extends ActionBarActivity {
	private final static String TAG = MainActivity1.class.getSimpleName();
	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		context = this;
		initTest2();
		initTest3();
	}

	private void initTest2() {
		CacheAsyncHttpClient client = new CacheAsyncHttpClient(
				new AsyncHttpClient(), context);
		String url = "http://test22.api.xsw0306.com/project_2_0/subject_recom/get_cate_curri?te=12&td=d&adc=sf&dsfds=adfdsew";
		String appid = "wxd930ea\"5d5a258f4f";
		String mch_id = "10000100";
		String device_info = "1000";
		String body = "test";
		String nonce_str = "ibuaiVcKdpRxkhJA";
		String test1 = "~._-*";
		String test4 = "!@#$%^&()+{}";
		String test2 = "\"\\/``";
		String test3 = ":|<>,?[];'";
		ArrayList<String> arrayList = new ArrayList<String>();
		arrayList.add("s234dfsa");
		arrayList.add("sdfs234a");
		arrayList.add("s23423dfsa");

		RequestParams parameters = new RequestParams();
		parameters.put("appid", appid);
		parameters.put("mch_id", mch_id);
		parameters.put("device_info", device_info);
		parameters.put("body", body);
		parameters.put("nonce_str", nonce_str);
		parameters.put("da", "");
		parameters.put("test1", test1);
		parameters.put("test4", test4);
		parameters.put("test2", test2);
		parameters.put("test3", test3);
		// parameters.put("arrayList", arrayList);
		try {
			parameters.put("file", new File("/storage/emulated/0/test.txt"));
			parameters
					.put("file1", new File("/storage/emulated/0/content.txt"));
			parameters.put("file1", new File("/storage/emulated/0/test.jpg"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// parameters.put("dad", "{\"da\":\"dd\",\"ddf\":\"真的\"}");
		// parameters
		// .put("dad2",
		// "http://file.cdnbos.bce.xsw0306.com/user/moments_img/1417_49972b6087d06f89.jpg");
		parameters
				.put("test",
						"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~._-*!@#$%^&()+{}:|<>,?=[];'\"\\/``");

		Log.i("parameters", parameters.toString());
		client.post(url, parameters, new CacheAsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode,
					org.apache.http.Header[] headers, byte[] responseBody) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onFailure(int statusCode,
					org.apache.http.Header[] headers, byte[] responseBody,
					Throwable error) {
				// TODO Auto-generated method stub

			}
		},true);
	}

	private void initTest3() {
		CacheAsyncHttpClient client = new CacheAsyncHttpClient(
				new AsyncHttpClient(), context);
		String url = "http://test22.api.xsw0306.com/project_2_1";
		File file = new File("/storage/emulated/0/test.jpg");
		InputStream instream = null;
		try {
			instream = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpEntity entity = new InputStreamEntity(instream, file.length());

		client.post(url, entity, new CacheAsyncHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					byte[] responseBody) {

			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					byte[] responseBody, Throwable error) {

			}
		},false);
	}

}
